import Vue from 'vue';
import VueRouter from 'vue-router';
import { Message } from 'element-ui';


//解决"Navigating to current location ("") is not allowed"报错
//原因: 在路由中添加了相同的路由。 常用于router-viwe手动刷新
const routerPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) { 
    return routerPush.call(this, location).catch(error=> error);
};

Vue.use(VueRouter);

let routes = [
    {
        path: '/',
        redirect: {
            name: 'Index'
        },
    },
    {
        path: '/shiro/logout',
        redirect: {
            name: 'Index'
        },
    },
    {
        path: '/xdeer_mall',
        redirect: {
            name: 'Login'
        },
    },
    {
        path: '/',
        component: () => import( /* webpackChunkName: "BasicLayout" */ '../views/Layout/BasicLayout.vue'),
        children: [
            {
                path: '/index',
                name: 'Index',
                component: () => import( /* webpackChunkName: "Index" */ '../views/Front/Index/index.vue')
            },
            {
                path: '/cart',
                name: 'Cart',
                component: () => import( /* webpackChunkName: "Cart" */ '../views/Front/Cart/index.vue')
            },
            {
                path: '/shop',
                name: 'Shop',
                component: () => import( /* webpackChunkName: "Shop" */ '../views/Front/Shop/index.vue')
            },
            {
                path: '/information',
                name: 'Information',
                component: () => import( /* webpackChunkName: "Information" */ '../views/Front/Information/index.vue')
            },
            {
                path: '/collect',
                name: 'Collect',
                component: () => import( /* webpackChunkName: "Collect" */ '../views/Front/Collect/index.vue')
            },
            {
                path: '/myInfo',
                name: 'MyInfo',
                component: () => import( /* webpackChunkName: "MyInfo" */ '../views/Front/MyInfo/index.vue')
            },
            {
                path: '/detail',
                name: 'Detail',
                component: () => import( /* webpackChunkName: "Goods" */ '../views/Front/Goods/index.vue')
            },
            {
                path: '/myorder',
                name: 'MyOrder',
                component: () => import( /* webpackChunkName: "MyOrder" */ '../views/Front/MyOrder/index.vue')
            },
            {
                path: '/myorderdetail',
                name: 'MyOrderDetail',
                beforeEnter: (to,from,next) => {
                    let userInfo = localStorage.userInfo && JSON.parse(localStorage.userInfo);
                    if(userInfo.user_idNumber){
                        next();
                    }else{
                        Message({
                            type: 'error',
                            showClose: true,
                            message: '请先进行实名制认证。'
                        });
                        next({ name: 'MyInfo', params: { select: '1-2' } });
                    }
                },
                component: () => import( /* webpackChunkName: "MyOrderDetail" */ '../views/Front/MyOrderDetail/index.vue')
            },
            {
                path: '/myfootmark',
                name: 'Myfootmark',
                component: () => import( /* webpackChunkName: "MyFootmark" */ '../views/Front/MyFootmark/index.vue')
            },
            {
                path: '/backhome',
                name: 'BackHome',
                component: () => import( /* webpackChunkName: "BackHome" */ '../views/Back/index.vue')
            },
        ],
    },
    {
        path: '/',
        component: () => import( /* webpackChunkName: "BlankLayout" */ '../views/Layout/BlankLayout.vue'),
        children: [
            {
                path: '/login',
                name: 'Login',
                component: () => import( /* webpackChunkName: "Login" */ '../views/Front/Login/index.vue')
            },
            {
                path: '/register',
                name: 'Register',
                component: () => import( /* webpackChunkName: "Register" */ '../views/Front/Register/index.vue')
            },
        ]
    },
    {
        path: '*',
        name: 'NotFound',
        component: () => import( /* webpackChunkName: "NotFound" */ '../views/NotFound.vue')
    },
];
//不需要验证是否登录的页面
let noAuthRouteName = ['Index','Shop', 'NotFound','Detail','Login','Register', 'MyOrderDetail'];
let storage = window.localStorage;
routes.forEach(route => {
    if(route.children){
        route.children.forEach(routeChild => {
            //不在noAuthRouteName的是需要登录才能进入的router
            if(!noAuthRouteName.includes(routeChild.name)){
                //加入验证是否登录的钩子
                routeChild.beforeEnter = function(to,from,next){
                    if(storage.userInfo){
                        next();
                    }else{
                        Message({
                            type: 'error',
                            showClose: true,
                            message: '请先登录'
                        });
                        next('/');
                    }
                }
            }
        })
    }
});
const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes,
    scrollBehavior() {
        return {
            x: 0,
            y: 0
        }
    }
});

export default router;